import datetime
from app.db.models.alert_groups import AlertGroup
from app.db.models.jobs import Job


class AlertGroupRollback:

    def service(self):
        """
        1、回退job数据；
        2、删除alert_group.alertSettings 字段
        3、回退默认告警策略明
        """
        # 回退job数据
        print(f"{datetime.datetime.now()} 开始回退告警数据......")
        jobs = []
        for job in jobs:
            Job.objects(pk=job.get('_id')).update(alertSettings__alertGroupIds=job.get('alertGroupIds', []))
        # 删除alert_group.alertSettings 字段
        AlertGroup.objects.update(unset__alertSettings=1)
        AlertGroup.objects(isDefault=True).update(name="默认组")
        print(f"{datetime.datetime.now()} 告警数据回退完成！！！")



